/**
 * 
 */
package com.antilia.infomaresme.pages.alert;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;

import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.InfomaresmeSession;
import com.antilia.infomaresme.data.dataobjects.CategoryAlert;
import com.antilia.infomaresme.data.dataobjects.User;
import com.antilia.infomaresme.pages.RoundButtonLinkPanel;
import com.antilia.infomaresme.pages.alert.edit.CategoryAlertEditPanel;
import com.antilia.infomaresme.pages.edit.ConfirmationPanel;
import com.antilia.infomaresme.provider.CategoryAlertsDataProvider;
import com.antilia.infomaresme.provider.UserLdm;
import com.antilia.infomaresme.resources.InfoModalWindow;

/**
 * @author Ernesto
 *
 */
public class CategoryAlertsPanel extends Panel {

	private static final long serialVersionUID = 1L;

	private CategoryAlertsDataProvider dataProvider;
	
	private AjaxFallbackDefaultDataTable<CategoryAlert> items;
	
	private ModalWindow modal;
	
	/**
	 * @param id
	 */
	public CategoryAlertsPanel(String id) {
		super(id);
		
		modal = new InfoModalWindow("modal");
		modal.setTitle("Detalles");
		modal.setInitialHeight(640);
		modal.setInitialWidth(950);
		modal.setResizable(false);
		add(modal);
		
		List<IColumn<CategoryAlert>> columns = new ArrayList<IColumn<CategoryAlert>>();	
		columns.add(new PropertyColumn<CategoryAlert>(Model.of(""), null , "price") {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void populateItem(
					org.apache.wicket.markup.repeater.Item<ICellPopulator<CategoryAlert>> item,
					String componentId, final IModel<CategoryAlert> rowModel) {
				
				ActionsPanel linksPanel = new ActionsPanel(componentId) {
					
					private static final long serialVersionUID = 1L;

					@Override
					protected void onDelete(AjaxRequestTarget target) {
						ConfirmationPanel confirmationPanel =new ConfirmationPanel(modal.getContentId(), Model.of("Borrar alerta?")) {
							
							private static final long serialVersionUID = 1L;

							@Override
							public void onOk(AjaxRequestTarget target) {
								InfomaresmeApplication.getQAApplication().getCategoryAlertsDao().delete(rowModel.getObject());
								modal.close(target);
								target.add(items);
							}
							
							@Override
							public void onCancel(AjaxRequestTarget target) {
								modal.close(target);
							}
						};
						modal.setInitialWidth(400);
						modal.setInitialHeight(200);
						modal.setTitle(new ResourceModel("actions.borrarAlerta"));
						modal.setContent(confirmationPanel);
						modal.show(target);
					}
					
					@Override
					protected void onEdit(AjaxRequestTarget target) {
						CategoryAlertEditPanel alertPanel = new CategoryAlertEditPanel(modal.getContentId(), new UserLdm(rowModel.getObject().getUser()),  rowModel.getObject()) {
							
							private static final long serialVersionUID = 1L;

							@Override
							public void onSaved(AjaxRequestTarget target, Form<?> form) {
								modal.close(target);
								target.add(items);
							}
						};
						modal.setTitle(new ResourceModel("actions.createAlert"));
						modal.setInitialWidth(900);
						modal.setInitialHeight(600);
						modal.setTitle(new ResourceModel("actions.createNewAlerta"));
						modal.setContent(alertPanel);
						modal.show(target);
					}
					
				};
				item.add(linksPanel);
			}
		});
		
		columns.add(new PropertyColumn<CategoryAlert>(new ResourceModel("title"), null, "title"));
		columns.add(new PropertyColumn<CategoryAlert>(new ResourceModel("events"), null, "events"));
		columns.add(new PropertyColumn<CategoryAlert>(new ResourceModel("item.categoryPath"), null, "categoryTitle"));
		columns.add(new PropertyColumn<CategoryAlert>(new ResourceModel("item.location"), null, "geographicLocation"));
		columns.add(new PropertyColumn<CategoryAlert>(new ResourceModel("emailFrequency"), null, "emailFrequency"));
		
		
		
 		dataProvider = new CategoryAlertsDataProvider() {
 			
 			private static final long serialVersionUID = 1L;

			 			
 			public User getUser() {
 				return InfomaresmeSession.getSession().getUser();
 			}; 			
 		
 		};		
		items  = new AjaxFallbackDefaultDataTable<CategoryAlert>("items", columns, dataProvider, 8);
		items.setOutputMarkupId(true);
		addOrReplace(items);
		
		RoundButtonLinkPanel create = new RoundButtonLinkPanel("create", new ResourceModel("actions.createAlert")) {
			
						private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				// create a new lert
				CategoryAlertEditPanel alertPanel = new CategoryAlertEditPanel(modal.getContentId(), new UserLdm(InfomaresmeSession.getSession().getUser()),  null) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onSaved(AjaxRequestTarget target, Form<?> form) {
						modal.close(target);
						target.add(items);
					}
				};
				modal.setTitle(new ResourceModel("actions.createAlert"));
				modal.setInitialWidth(900);
				modal.setInitialHeight(600);
				modal.setTitle(new ResourceModel("actions.createNewAlerta"));
				modal.setContent(alertPanel);
				modal.show(target);
			}
		};
		add(create);
	}
}
